<?php

namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;


class Con2 extends AdminController
{
    public function index()
    {
        $list=Db::table('cc_admin_role')->field(['r_id', 'r_name'])->order('r_id', 'ASC')->select();
//        dump($list);die;
        foreach ($list as $v) {
            $n_ids = Db::table('cc_role_node')->field('n_id')->where('r_id', $v['r_id'])->select();
//            dump($n_ids);
            //定义空数组
            $nodes = array();
            //遍历
            foreach ($n_ids as $value) {
                $nodes[] = Db::table('cc_admin_node')->where('n_id', $value['n_id'])->field('n_name')->find();
            }
            $v['node'] = $nodes; //将新得到角色信息放置到$v中
//            dump($roles);
            $arr[] = $v;
//            dump($arr);die;
        }
        return view('admin@con2/tables',['list' => $arr]);
    }

    public function add()
    {
        return view('admin@con2/add');
    }

    public function doAdd(Request $request)
    {
        $p = input('get.'); // 获取GET数据
//        dump($p);die;
        $data = [
            'r_name'     => $p['name']
        ];
        $result = Db::table('cc_admin_role')->data($data)->insert();

        if ($result > 0) {
            return $this->success('添加成功!', url('admin/con2/index'));
        } else {
            return $this->error('添加失败!');
        }

    }

    public function edit($id)
    {
        $row = Db::table('cc_admin_role')->find($id);
//        dump($row);die;
        return view('admin@con2/edit',['row'=>$row]);
    }

    public function doEdit(Request $request,$id)
    {
        $info = input('get.');

//        dump($info);die;

        // 手动处理数据
        $data = [
            'r_name'     => $info['r_name']
        ];

        $result = Db::table('cc_admin_role')->where('r_id', $id)->update($data);

        if ($result > 0) {
            return $this->success('更新成功!', url('admin/con2/index'));
        } else {
            return $this->error('更新失败!');
        }
    }

    public function node($id)
    {
        $role = Db::table('cc_admin_role')->where('r_id',$id)->find();
//        dump($role);die;
        $node = Db::table('cc_admin_node')->select();
//        dump($node);die;
        return view('admin@con2/node',['role'=>$role,'node'=>$node]);
    }

    /**
     * 处理权限更改数据，将数据添加到数据库
     *
     * @param Request $request
     * @param $r_id
     */
    public function doNode(Request $request,$r_id)
    {
        $info = $request->put();
//        dump($info);die;

        // 判断是否选择了权限
        if (!empty($info['node'])){
            // 删除 权限角色表中 所有 此ID下得数据
            Db::table('cc_role_node')->where('r_id',$r_id)->delete($r_id);
            // 获取已选择得权限
            $data = $info['node'];
//            dump($data);die;
            // 获取 权限 ID
            foreach ($data as $k) {
                $n_id[] = $k;
            }
            $role_node = '';
            for ($i=0;$i<sizeof($n_id);$i++) {
                $data = [
                    'r_id' => $r_id,
                    'n_id' => $n_id[$i]
                ];
                $result = Db::table('cc_role_node')->data($data)->insert();
            }
            if ($result > 0) {
                return $this->success('权限更新成功!', url('admin/Con2/index'));
            } else {
                return $this->error('权限更新失败!');
            }
        }else{
            return $this->error ('未选择角色,修改失败!');
        }
    }
}
